package edu.ncu.controller;

import edu.ncu.entity.News;
import edu.ncu.entity.User;
import edu.ncu.service.NewsService;
import edu.ncu.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    final UserService userService;
    final NewsService newsService;

    public AdminController(UserService userService, NewsService newsService) {
        this.userService = userService;
        this.newsService = newsService;
    }

    //查询所有用户信息
    @RequestMapping("/findAllUsers")
    public String findAll(Model model, ModelMap map)
    {
        System.out.println("表现层:findAll方法执行了...");
        //调用service方法,需要service层的对象,需要注入
        List<User> users = userService.findAll();
        System.out.println(users);
        model.addAttribute("users",users);
        return "/admin";
    }

    //更改用户状态
    @RequestMapping("/updateUserState")
    public String updateUserState(int state, int userId)
    {
        System.out.println("更改的用户状态：userId: "+userId+" state: "+state);
        userService.updateUserState(state, userId);
        return "/admin";
    }

    //发布新闻
    @RequestMapping("/saveNews")
    public String saveNews(News news)
    {
        System.out.println("要保存的新闻：" + news);
        newsService.saveNews(news);
        return "redirect:/news/findAll";
    }

    //删除新闻
    @RequestMapping("/delete")
    public String delete(int newsId)
    {
        System.out.println("要删除的新闻id：" + newsId);
        newsService.deleteNews(newsId);
        return "redirect:/news/findAll";
    }

}

